package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.api.BusinessCollect;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.Constant;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.PersonalBaseInfoVO;
import com.digitalchina.lxnsy.entity.ShareFarmListVO;
import com.digitalchina.lxnsy.fragment.ShopActivityFragment;
import com.digitalchina.lxnsy.fragment.ShopShareFragment;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.facebook.drawee.view.SimpleDraweeView;
import com.kekstudio.dachshundtablayout.DachshundTabLayout;
import com.kekstudio.dachshundtablayout.indicators.LineMoveIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/9/4 16:58
 * 修改备注：
 * ================================================
 */
public class ShareShopActivity extends BaseActivity {
    @Bind(R.id.sdv_head)
    SimpleDraweeView sdvHead;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_collect)
    TextView tvCollect;
    @Bind(R.id.tv_consultation)
    TextView tvConsultation;
    @Bind(R.id.tab_layout)
    DachshundTabLayout tabLayout;
    @Bind(R.id.viewPager)
    ViewPager viewPager;

    /*每个 tab 的 item*/
    private MyPagerAdapter adapter;
    private List<Fragment> fragments = new ArrayList<>();
    private String title[] = {"共享", "活动"};

    private ShopShareFragment f1 = new ShopShareFragment();
    private ShopActivityFragment f2 = new ShopActivityFragment();


    private String userAccId = "";

    private PersonalBaseInfoVO personalBaseInfoVO;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_share_shop);
        setTitle("店铺");
    }

    @Override
    public void initMember() {

        if (getIntent().hasExtra("ShareFarmListVO")) {
            ShareFarmListVO vo = (ShareFarmListVO) getIntent().getSerializableExtra("ShareFarmListVO");
            userAccId = vo.getUserAccId();
            setView();
        } else {
            userAccId = (String) getIntent().getSerializableExtra("contentId");
            setView();
        }

    }

    private void setView() {
        BusinessAccount.getPersonInfoById(this, userAccId, mHandler);


        tabLayout.setAnimatedIndicator(new LineMoveIndicator(tabLayout));

        fragments.add(f1);
        fragments.add(f2);

        Bundle bundle = new Bundle();
        bundle.putString("userAccId", userAccId);
        f1.setArguments(bundle);
        f2.setArguments(bundle);

        adapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setOffscreenPageLimit(title.length);
        viewPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);


                switch (msg.what) {
                    case MSG.MSG_GET_PERSON_INFO_SUCCESS: // 个人信息
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_PERSON_INFO_FIELD:
                        break;
                    case MSG.MSG_ADD_STORE_SUCCESS:
                        showToast("收藏成功");
                        break;
                    case MSG.MSG_ADD_STORE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        personalBaseInfoVO = FastJsonUtil.getBean(object.toString(), "personalBaseInfo", PersonalBaseInfoVO.class);

        if (personalBaseInfoVO == null)
            return;


        if (!StringUtil.isStrEmpty(personalBaseInfoVO.getHeadPortrait()))
            FrescoUtil.showImageMid(personalBaseInfoVO.getHeadPortrait(), sdvHead);

        tvName.setText(personalBaseInfoVO.getUserName());

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_collect, R.id.tv_consultation})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_collect:
                if (personalBaseInfoVO != null)
                    BusinessCollect.addStore(ShareShopActivity.this, userAccId, "", mHandler);
                break;
            case R.id.tv_consultation:
                break;
        }
    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {
        private MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }

    }
}
